$(function () {
    var deathTableData = [];//全部数据数组
    var searchDeathTableData = [];//查询条件存放的数组
    deathTableData = [
        {
            id: 25634,
            name: '陈舒珠',
            identity: '440106189811128213',
            sex: '女',
            birthDate: '18981112',
            village: '车坡街美好社区',
            deathDate: '2017-08-23',
            regisDeathDate: '2017-09-21',
            dataSouce: '城乡居民养老保险',
            auditState: '未登记',
            auditState1: 0
        }, {
            id: 25635,
            name: '周保银',
            identity: '340111199807125218',
            sex: '男',
            birthDate: '19930802',
            village: '车坡街西岸社区',
            deathDate: '2011-01-01',
            regisDeathDate: '2011-01-01',
            dataSouce: '城镇职工医疗保险',
            auditState: '处理中',
            auditState1: 1
        }, {
            id: 25636,
            name: '张三',
            identity: '510102xxxxxxxx6317',
            sex: '男',
            birthDate: '18980302',
            village: '车坡街美好社区',
            deathDate: '2006-08-23',
            regisDeathDate: '2007-09-21',
            dataSouce: '城乡居民养老保险',
            auditState: '未登记',
            auditState1: 0
        }, {
            id: 25637,
            name: '林高兴',
            identity: '440106xxxxxxxx1817',
            sex: '男',
            birthDate: '18980802',
            village: '车坡街美好社区',
            deathDate: '2017-08-23',
            regisDeathDate: '2017-09-21',
            dataSouce: '城镇职工医疗保险',
            auditState: '已处理',
            auditState1: 2
        }, {
            id: 25638,
            name: '黄二妹',
            identity: '430111xxxxxxxx4811',
            sex: '女',
            birthDate: '18990802',
            village: '车坡街美好社区',
            deathDate: '1898-08-23',
            regisDeathDate: '2017-09-21',
            dataSouce: '城乡居民养老保险',
            auditState: '未登记',
            auditState1: 0
        }, {
            id: 25639,
            name: '林冬菊',
            identity: '441522xxxxxxxx2765',
            sex: '女',
            birthDate: '17860802',
            village: '车坡街美好社区',
            deathDate: '2007-08-23',
            regisDeathDate: '2007-08-24',
            dataSouce: '城乡居民养老保险',
            auditState: '未登记',
            auditState1: 0
        }, {
            id: 25640,
            name: '欧连发',
            identity: '460003xxxxxxxx7814',
            sex: '男',
            birthDate: '18980802',
            village: '车坡街美好社区',
            deathDate: '2001-08-23',
            regisDeathDate: '2002-09-21',
            dataSouce: '城镇职工医疗保险',
            auditState: '已处理',
            auditState1: 2
        }, {
            id: 25641,
            name: '卢水妹',
            identity: '440102xxxxxxxx7018',
            sex: '女',
            birthDate: '18980802',
            village: '车坡街美好社区',
            deathDate: '2017-08-23',
            regisDeathDate: '2017-09-21',
            dataSouce: '城乡居民养老保险',
            auditState: '未登记',
            auditState1: 0
        }, {
            id: 25642,
            name: '陈美英',
            identity: '440582xxxxxxxx5038',
            sex: '女',
            birthDate: '19020706',
            village: '车坡街美好社区',
            deathDate: '2004-02-21',
            regisDeathDate: '2011-09-21',
            dataSouce: '城镇职工医疗保险',
            auditState: '处理中',
            auditState1: 1
        }, {
            id: 25643,
            name: '郭红燕',
            identity: '441422xxxxxxxx5617',
            sex: '女',
            birthDate: '19760802',
            village: '车坡街美好社区',
            deathDate: '2000-01-21',
            regisDeathDate: '2003-09-21',
            dataSouce: '城乡居民养老保险',
            auditState: '已处理',
            auditState1: 2
        }
    ];
    var ifSearchAll = true;
    //身份证对象--演示用
    var idenifyObject = $("[name='idenify']");
    //得到从父级页面过来的参数
    function getParamFromParent() {
        var identity = sessionStorage.getItem("identity");
        console.log(identity);
        if (identity) {
            idenifyObject.val(identity);
        }
    }

    //初始化死亡人员表格
    var deathTable = $("#deathTable").DataTable({
        searching: false,
        ordering: false,
        lengthChange: false,
        data: deathTableData,
        //国际化
        language: {
            search: "在表格中搜索:",
            "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "infoEmpty": "没有记录可以显示",
            "paginate": {
                "first": "首页",
                "previous": "上页",
                "next": "下页",
                "last": "末页"
            },
            "emptyTable": "无可用数据"
        },
        columns: [{
            data: 'name',
            title: '姓名',
            width: '8%'
        }, {
            data: 'identity',
            title: '身份证号码',
            width: '10%'
        }, {
            data: 'sex',
            title: '性别',
            width: '6%'
        }, {
            data: 'birthDate',
            title: '出生日期',
            width: '10%'
        }, {
            data: 'village',
            title: '村（社区）',
            width: '15%'
        }, {
            data: 'deathDate',
            title: '死亡日期',
            width: '10%'
        }, {
            data: 'regisDeathDate',
            title: '登记死亡日期',
            width: '12%'
        }, {
            data: 'dataSouce',
            title: '数据来源',
            width: '13%'
        }, {
            data: 'auditState',
            title: '受理状态',
            width: '12%',
            className: 'textState'
        }, {
            data: 'id',
            render: function (data, type, row, meta) {
                //console.log(row);
                /*if(parseInt(row.auditState1) != 0){
                 return "<button class='layui-btn layui-btn-normal layui-disabled regisAudit' value='" + data + "'>稽核登记</button>"
                 }*/
                return "<button class='layui-btn layui-btn-normal regisAudit' value='" + data + "'>问题受理</button>"
            },
            title: '操作',
            width: '8%'
        }]
    });

    var desc = $("[name='desc']");//问题描述
    var acceptWay = $("[name='acceptWay']");//受理方式
    //var acceptWayInput = $("[name='acceptWayInput']");//受理方式输入框
    var infoSourType = $("[name='infoSourType']");//信息来源方类型
    // var infoSourTypeIn = $("[name='infoSourTypeIn']"); //信息来源方类型输入框
    var acceptDate = $("[name='acceptDate']");//受理日期
    var acceptDate1 = $("[name='acceptDate1']");//受理日期
    var infoSour = $("[name='infoSour']");//信息来源方式
    //var infoSourIn = $("[name='infoSourIn']");//信息来源方式输入框
    var address = $("[name='address']");//地址
    var postcode = $("[name='postcode']");//邮编
    var tell = $("[name='tell']");//联系方式
    var mailbox = $("[name='mailbox']");//联系邮箱
	
	//layui的使用
    layui.use(['jquery', 'form', 'layer', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate;
        var layer_accept_question = null;
        //自定义校验规则
        form.verify({
            idenify: [/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, '请输入有效的证件号'],
            tell: [/^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/, '请输入正确的联系方式'],
            mailbox: [/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/, '请输入有效邮箱地址']
        });
        //监听提交
        form.on('submit(demo1)', function (data) {
            //console.log(data);
            //var death_audit_load = layer.load();
            var submitDate = eval(data.field);
            if (submitDate.auditType || submitDate.idenify || submitDate.quiz1 || submitDate.quiz2 || submitDate.acceptType2) {
                //layer.alert("请至少选择一个查询项");
                ifSearchAll = false;
                //return false;
            }else{
                ifSearchAll = true;
            }
            searchDeathTableData = [];//每次查询都清空一次数组

            $.each(deathTableData, function (index, item) {
                if (submitDate.acceptType2 && submitDate.acceptType2 == item.auditState && submitDate.idenify == "") {
                    searchDeathTableData.push(item);
                } else if (submitDate.idenify && submitDate.idenify == item.identity && submitDate.acceptType2 == "") {
                    searchDeathTableData.push(item);
                } else if (submitDate.idenify == item.identity && submitDate.acceptType2 == item.auditState) {
                    searchDeathTableData.push(item);
                }
            });
            //销毁表格
            if (deathTable != null) {
                deathTable.clear();
                deathTable.destroy();
            }
            deathTable = $("#deathTable").DataTable({
                searching: false,
                ordering: false,
                lengthChange: false,
                data: ifSearchAll ? deathTableData : searchDeathTableData,
                //国际化
                language: {
                    search: "在表格中搜索:",
                    "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                    "infoEmpty": "没有记录可以显示",
                    "paginate": {
                        "first": "首页",
                        "previous": "上页",
                        "next": "下页",
                        "last": "末页"
                    },
                    "emptyTable": "无可用数据"
                },
                columns: [{
                    data: 'name',
                    title: '姓名',
                    width: '8%'
                }, {
                    data: 'identity',
                    title: '身份证号码',
                    width: '12%'
                }, {
                    data: 'sex',
                    title: '性别',
                    width: '6%'
                }, {
                    data: 'birthDate',
                    title: '出生日期',
                    width: '10%'
                }, {
                    data: 'village',
                    title: '村（社区）',
                    width: '15%'
                }, {
                    data: 'deathDate',
                    title: '死亡日期',
                    width: '10%'
                }, {
                    data: 'regisDeathDate',
                    title: '登记死亡日期',
                    width: '12%'
                }, {
                    data: 'dataSouce',
                    title: '数据来源',
                    width: '13%'
                }, {
                    data: 'auditState',
                    title: '受理状态',
                    width: '8%',
                    className: 'textState'
                }, {
                    data: 'id',
                    render: function (data, type, row, meta) {
                        return "<button class='layui-btn layui-btn-normal regisAudit' value='" + data + "'>问题受理</button>"
                    },
                    title: '操作',
                    width: '8%'
                }]
            });

            openAcceptQuest();
            return false;
        });
        //受理日期
        laydate.render({
            elem: '#acceptDate',
            format: "yyyyMMdd",
            value: new Date()
        });

        //打开问题受理弹窗
        function openAcceptQuest() {
            $(".regisAudit").on("click", function () {
                //得到点击对象完整信息
                var current_index = $(this).attr("value");
                var data = [];
                var index;
                if (searchDeathTableData.length > 0) {
                    for (var i = 0; i < searchDeathTableData.length; i++) {
                        if (searchDeathTableData[i].id == current_index) {
                            data.push(searchDeathTableData[i]);
                            index = i;
                            break;
                        }
                    }
                } else {
                    for (var i = 0; i < deathTableData.length; i++) {
                        if (deathTableData[i].id == current_index) {
                            data.push(deathTableData[i]);
                            index = i;
                            break;
                        }
                    }
                }
                //console.log(data);
                if (data.length > 0) {
                    var obj = data[0];
                    if (obj.auditState1 == 0) {
                        desc.text("该人员：" + obj.name + "，性别：" + obj.sex + "，证件号码：" + obj.identity + "；");
                        desc.removeAttr("readonly");
                        $(acceptWay).next().find("input").removeClass("layui-disabled");
                        $(infoSourType).next().find("input").removeClass("layui-disabled");
                        acceptDate.show();
                        acceptDate1.hide();
                        $(infoSour).next().find("input").removeClass("layui-disabled");
                        address.removeAttr("readonly");
                        postcode.removeAttr("readonly");
                        tell.removeAttr("readonly");
                        mailbox.removeAttr("readonly");
                        $("#save").show();
                        $("#disSave").hide();
                    } else {
                        var value = acceptDate.val();
                        desc.attr("readonly", "true");
                        $(acceptWay).next().find("input").addClass("layui-disabled");
                        $(infoSourType).next().find("input").addClass("layui-disabled");
                        acceptDate1.val(value).show();
                        acceptDate.hide();
                        $(infoSour).next().find("input").addClass("layui-disabled");
                        address.attr("readonly", "true");
                        postcode.attr("readonly", "true");
                        tell.attr("readonly", "true");
                        mailbox.attr("readonly", "true");
                        //console.log(value);
                        if (obj.desc) {
                            desc.text(obj.desc);
                        }
                        if (obj.acceptWay) {
                            $(acceptWay).next().find("input").val(obj.acceptWay);
                        }
                        if (obj.infoSourType) {
                            $(infoSourType).next().find("input").val(obj.acceptWay);
                        }
                        if (obj.acceptDate) {
                            $(acceptDate).val(obj.acceptDate);
                        }
                        if (obj.infoSour) {
                            $(infoSour).next().find("input").val(obj.infoSour);
                        }
                        if (obj.address) {
                            address.val(obj.address);
                        }
                        if (obj.postcode) {
                            postcode.val(obj.postcode);
                        }
                        if (obj.tell) {
                            tell.val(obj.tell);
                        }
                        if (obj.mailbox) {
                            mailbox.text(obj.mailbox);
                        }


                        $("#save").hide();
                        $("#disSave").show();
                    }

                }
                //console.log(deathTable);
                layer_accept_question = layer.open({
                    type: 1,
                    content: $("#layer-register-audit"),
                    area: ['1000px', '550px'],
                    title: '问题受理',
                    /*cancel:function(){
                     searchDeathTableData
                     }*/
                });
                form.on('submit(demo2)', function (formData) {
                    console.log(ifSearchAll);
                    var submitData = formData.field;
                    if (ifSearchAll) {
                        deathTableData[index]['desc'] = submitData.desc;
                        deathTableData[index]['acceptWay'] = submitData.acceptWay;
                        deathTableData[index]['infoSourType'] = submitData.infoSourType;
                        deathTableData[index]['acceptDate'] = submitData.acceptDate;
                        deathTableData[index]['infoSour'] = submitData.infoSour;
                        deathTableData[index]['address'] = submitData.address;
                        deathTableData[index]['postcode'] = submitData.postcode;
                        deathTableData[index]['tell'] = submitData.tell;
                        deathTableData[index]['mailbox'] = submitData.mailbox;
                        deathTableData[index]['auditState'] = '处理中';
                        deathTableData[index]['auditState1'] = 1;
                    } else {
                        searchDeathTableData[index]['desc'] = submitData.desc;
                        searchDeathTableData[index]['acceptWay'] = submitData.acceptWay;
                        searchDeathTableData[index]['infoSourType'] = submitData.infoSourType;
                        searchDeathTableData[index]['acceptDate'] = submitData.acceptDate;
                        searchDeathTableData[index]['infoSour'] = submitData.infoSour;
                        searchDeathTableData[index]['address'] = submitData.address;
                        searchDeathTableData[index]['postcode'] = submitData.postcode;
                        searchDeathTableData[index]['tell'] = submitData.tell;
                        searchDeathTableData[index]['mailbox'] = submitData.mailbox;
                        searchDeathTableData[index]['auditState'] = '处理中';
                        searchDeathTableData[index]['auditState1'] = 1;
                    }
                    //console.log(JSON.stringify(deathTableData[index]));
                    //销毁表格
                    if (deathTable != null) {
                        deathTable.clear();
                        deathTable.destroy();
                    }
                    deathTable = $("#deathTable").DataTable({
                        searching: false,
                        ordering: false,
                        lengthChange: false,
                        data: ifSearchAll ? deathTableData : searchDeathTableData,
                        //国际化
                        language: {
                            search: "在表格中搜索:",
                            "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                            "infoEmpty": "没有记录可以显示",
                            "paginate": {
                                "first": "首页",
                                "previous": "上页",
                                "next": "下页",
                                "last": "末页"
                            },
                            "emptyTable": "无可用数据"
                        },
                        columns: [{
                            data: 'name',
                            title: '姓名',
                            width: '8%'
                        }, {
                            data: 'identity',
                            title: '身份证号码',
                            width: '12%'
                        }, {
                            data: 'sex',
                            title: '性别',
                            width: '6%'
                        }, {
                            data: 'birthDate',
                            title: '出生日期',
                            width: '10%'
                        }, {
                            data: 'village',
                            title: '村（社区）',
                            width: '15%'
                        }, {
                            data: 'deathDate',
                            title: '死亡日期',
                            width: '10%'
                        }, {
                            data: 'regisDeathDate',
                            title: '登记死亡日期',
                            width: '12%'
                        }, {
                            data: 'dataSouce',
                            title: '数据来源',
                            width: '13%'
                        }, {
                            data: 'auditState',
                            title: '受理状态',
                            width: '8%',
                            className: 'textState'
                        }, {
                            data: 'id',
                            render: function (data, type, row, meta) {
                                return "<button class='layui-btn layui-btn-normal regisAudit' value='" + data + "'>问题受理</button>"
                            },
                            title: '操作',
                            width: '8%'
                        }]
                    });
                    deathTable.draw();
                    /*$.each(deathTable.data(),function(index,item){
                     console.log(item);
                     })*/
                    layer.msg("保存成功");
                    layer.close(layer_accept_question);
                    openAcceptQuest();
                    return false;
                });
            });
        };

        //关闭弹窗
        $(".close").on("click", function () {
            layer.close(layer_accept_question);
            return false;
        });
        //初始化点击事件
        function initEvent() {
            openAcceptQuest();
            getParamFromParent();
        };
        //入口函数
        initEvent();
    });
});